package com.itcam.lease.web.admin.service.impl;

import com.itcam.lease.common.exception.LeaseException;
import com.itcam.lease.common.result.ResultCodeEnum;
import com.itcam.lease.model.entity.*;
import com.itcam.lease.model.enums.ItemType;
import com.itcam.lease.web.admin.mapper.*;
import com.itcam.lease.web.admin.service.*;
import com.itcam.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.itcam.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.itcam.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.itcam.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.itcam.lease.web.admin.vo.fee.FeeValueVo;
import com.itcam.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * 保存或更新公寓信息
     * @param apartmentSubmitVo
     */
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        // 1.判断是否是更新操作
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);

        // 2.如果是更新操作
        if (isUpdate) {
            // 1.1 删除图片列表
            // 创建一个查询包装器，用于过滤出属于该公寓的图片
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：图片类型为公寓，且图片所属的公寓ID与提交的公寓ID相同
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            // 调用服务的删除方法，删除符合条件的图片记录
            graphInfoService.remove(graphQueryWrapper);

            // 1.2 删除配套列表
            // 创建一个查询包装器，用于过滤出属于该公寓的配套设施
            LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：配套设施所属的公寓ID与提交的公寓ID相同
            facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            // 调用服务的删除方法，删除符合条件的配套设施记录
            apartmentFacilityService.remove(facilityQueryWrapper);

            // 1.3 删除标签列表
            // 创建一个查询包装器，用于过滤出属于该公寓的标签
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：标签所属的公寓ID与提交的公寓ID相同
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            // 调用服务的删除方法，删除符合条件的标签记录
            apartmentLabelService.remove(labelQueryWrapper);

            // 1.4 删除杂费列表
            // 创建一个查询包装器，用于过滤出属于该公寓的杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：杂费所属的公寓ID与提交的公寓ID相同
            feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            // 调用服务的删除方法，删除符合条件的杂费记录
            apartmentFeeValueService.remove(feeQueryWrapper);
        }

        // 3.新增或删除后重新添加公寓信息
        // 3.1 插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                // 创建一个GraphInfo对象
                GraphInfo graphInfo = new GraphInfo();
                // 设置图片类型为公寓
                graphInfo.setItemType(ItemType.APARTMENT);
                // 设置图片所属的公寓ID
                graphInfo.setItemId(apartmentSubmitVo.getId());
                // 设置图片名称
                graphInfo.setName(graphVo.getName());
                // 设置图片URL
                graphInfo.setUrl(graphVo.getUrl());
                // 将图片信息添加到列表中
                graphInfoList.add(graphInfo);
            }
            // 批量保存图片信息
            graphInfoService.saveBatch(graphInfoList);
        }
        // 3.2 插入配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            List<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                // 创建一个ApartmentFacility对象
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                // 设置配套设施所属的公寓ID
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                // 设置配套设施ID
                apartmentFacility.setFacilityId(facilityId);
                // 将配套设施信息添加到列表中
                facilityList.add(apartmentFacility);
            }
            // 批量保存配套设施信息
            apartmentFacilityService.saveBatch(facilityList);
        }
        // 3.3 插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                // 创建一个ApartmentLabel对象
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                // 设置标签所属的公寓ID
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                // 设置标签ID
                apartmentLabel.setLabelId(labelId);
                // 将标签信息添加到列表中
                apartmentLabelList.add(apartmentLabel);
            }
            // 批量保存标签信息
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
        // 3.4 插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                // 创建一个ApartmentFeeValue对象
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                // 设置杂费所属的公寓ID
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                // 设置杂费ID
                apartmentFeeValue.setFeeValueId(feeValueId);
                // 将杂费信息添加到列表中
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            // 批量保存杂费信息
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }


    }

    /**
     * 根据条件分页查询公寓列表
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    /**
     * 根据ID获取公寓详细信息
     * @param id
     * @return
     */
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        // 1.查询ApartmentInfo
        ApartmentInfo apartmentInfo = this.getById(id);
        // 如果公寓信息不存在，则返回null
        if (apartmentInfo == null) {
            return null;
        }
        // 2.查询GraphInfo
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        // 3.查询LabelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        // 4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        // 5.查询FeeValueVo
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        // 创建一个ApartmentDetailVo对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        // 将apartmentInfo的属性复制到apartmentDetailVo中
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        // 设置公寓的图片信息
        apartmentDetailVo.setGraphVoList(graphVoList);
        // 设置公寓的标签信息
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        // 设置公寓的配套设施信息
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        // 设置公寓的杂费信息
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        // 返回公寓的详细信息
        return apartmentDetailVo;

    }

    /**
     * 根据id删除公寓信息
     * @param id
     */
    @Override
    public void removeApartmentById(Long id) {
        super.removeById(id);

        // 由于公寓下会包含房间信息，因此在删除公寓时最好先判断一下该公寓下是否存在房间信息，若存在，则提醒用户先删除房间信息后再删除公寓信息
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            // 1. 删除图片列表
            // 创建一个查询包装器，用于过滤出属于该公寓的图片
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：图片类型为公寓，且图片所属的公寓ID与提交的公寓ID相同
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphQueryWrapper.eq(GraphInfo::getItemId, id);
            // 调用服务的删除方法，删除符合条件的图片记录
            graphInfoService.remove(graphQueryWrapper);

            // 2. 删除配套列表
            // 创建一个查询包装器，用于过滤出属于该公寓的配套设施
            LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：配套设施所属的公寓ID与提交的公寓ID相同
            facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
            // 调用服务的删除方法，删除符合条件的配套设施记录
            apartmentFacilityService.remove(facilityQueryWrapper);

            // 3. 删除标签列表
            // 创建一个查询包装器，用于过滤出属于该公寓的标签
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：标签所属的公寓ID与提交的公寓ID相同
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
            // 调用服务的删除方法，删除符合条件的标签记录
            apartmentLabelService.remove(labelQueryWrapper);

            // 4. 删除杂费列表
            // 创建一个查询包装器，用于过滤出属于该公寓的杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
            // 设置过滤条件：杂费所属的公寓ID与提交的公寓ID相同
            feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            // 调用服务的删除方法，删除符合条件的杂费记录
            apartmentFeeValueService.remove(feeQueryWrapper);
        }
    }


}




